using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NorthwindWeb.Models;

namespace NorthwindWeb.Controllers
{   
    public class CustomerDemographicController : Controller
    {
		private readonly ICustomerDemographicRepository customerdemographicRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public CustomerDemographicController() : this(new CustomerDemographicRepository())
        {
        }

        public CustomerDemographicController(ICustomerDemographicRepository customerdemographicRepository)
        {
			this.customerdemographicRepository = customerdemographicRepository;
        }

        //
        // GET: /CustomerDemographic/

        public ViewResult Index()
        {
            return View(customerdemographicRepository.AllIncluding(customerdemographic => customerdemographic.Customers));
        }

        //
        // POST: /CustomerDemographic/Create

        [HttpPost]
        public ActionResult Create(CustomerDemographic customerdemographic)
        {
            if (ModelState.IsValid) {
                customerdemographicRepository.InsertOrUpdate(customerdemographic);
                customerdemographicRepository.Save();
                return RedirectToAction("Index");
            } else {
                return RedirectToAction("Index");
            }
        }

        //
        // POST: /CustomerDemographic/Edit/5

        [HttpPost]
        public ActionResult Edit(CustomerDemographic customerdemographic)
        {
            if (ModelState.IsValid) {
                customerdemographicRepository.InsertOrUpdate(customerdemographic);
                customerdemographicRepository.Save();
                return RedirectToAction("Index");
            } else {
                return RedirectToAction("Index");
            }
        }

        //
        // POST: /CustomerDemographic/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            customerdemographicRepository.Delete(id);
            customerdemographicRepository.Save();

            return RedirectToAction("Index");
        }

        
        public JsonResult customerdemographicGridData(int rows, int page)
        {
        	var count = customerdemographicRepository.All.Count();
            var pageData = customerdemographicRepository.All.OrderBy(x => x.CustomerTypeID).Skip((page - 1) * rows).Take(rows).Select(o => new {CustomerTypeID = o.CustomerTypeID, CustomerDesc = o.CustomerDesc});
            return Json(new { 
            page = page,
            records = count,
            rows = pageData,
            total = Math.Ceiling((decimal)count / rows)
            }, JsonRequestBehavior.AllowGet);
        }
        
        
    }
}

